<?php
require_once dirname(__FILE__) . '/Migrate.abstract.php';
class WFMigrate20to30 extends AMigrate
{
    /**
     *\brief Desciption
     * This method is main() function of Migration WF20 to WF30
     * @see AMigrate::execute()
     */
    public function execute(){
        $strPathPidFile = $this->objWidgetPoolFactory->getWFHome() . '/repository/migrate20to30.pid';
        $objMutex = $this->objWidgetPoolFactory->getIOFactory()->getMutex($strPathPidFile);
        //Step1: Start checking is Migrate WF2.0 to WF3.0 process is mutex!
        if(true === $objMutex->isRunning()){
            $msg = __CLASS__ . '::' . __FUNCTION__ . '()=>Another Update process is running!It is not allowed concurrent Migrate WF2.0 to WF3.0 proccess!';
            throw new WFMigrateMigrateException($msg, WFOpt::UPGRADE_WF_FAILED_IS_NOT_MUTEX);
        }
        $msg = '>>>>Step '.$this->getStepNoAccumulate().': Success checking is Migrate WF2.0 to WF3.0 process is mutex!';
        $this->objLogger->info($msg);
        
        $this->doStepCheckFolderWidgetOldExisted();
        
        $this->doStepCheckWFDB();
        
        $this->doStepInitWFDBSchema();
        
        $this->doStepSynWidgetXMLToDB();
        
        $this->doStepSynWidgetComponentXMLToDB();
        
        $this->doStepSynServerListType();
        
        $this->migrateData();
    }
    /**
     *\brief Desciption
     * This method is Table users Migration WF20 to WF30
     */
    protected function getArrDataUsers20to30ByArrDataFrom($arrDataFrom){
        global $wfconf_default_user_cdata;
        //$arrDataFrom['cdata'] = '[{"container_id":1,"container_name":"dashBoard","container_param":{"serverList":true,"help":true,"tabSettings":true,"addWidgets":true,"tab":true,"addTab":true},"container_type_name":"dashBoard"},{"container_id":2,"container_name":"updateConfigureWidgetAndWidgetComponent","container_param":{"serverList":false,"help":false,"tabSettings":false,"addWidgets":false,"tab":false,"addTab":false},"container_type_name":"updateConfigureWidgetAndWidgetComponent"}]';
        $arrDataFrom['cdata'] = $wfconf_default_user_cdata;
        $arrsDataFromPdata = json_decode($arrDataFrom['pdata'],true);
        
        $arrsDataFromPdataForWF30 = array();
        if(is_array($arrsDataFromPdata) && $arrsDataFromPdata){
            foreach($arrsDataFromPdata as $assocDataFromPdata){
                $assocDataFromPdata['container_id'] = 1;
                $arrsDataFromPdataForWF30[] = $assocDataFromPdata;
            }
        }
        $jsonPdataAppend = '{"sqeuenceNumber":1,"pageName":"updateConfigureWidgetAndWidgetComponent","layout":"1","pageNumber":14,"boxWidth":"","autoFit":0,"container_id":2}';
        $arrsDataFromPdataForWF30[] = json_decode($jsonPdataAppend, true);
        $arrDataFrom['pdata'] = json_encode($arrsDataFromPdataForWF30);
        return $arrDataFrom;
    }
    
    /**
     *\brief Desciption
     * This method will get maximun wid from jsonUsersUdata
     */
    protected function getMaxWidByJsonUsersUdata($jsonUsersUdata){
        $ret = null;
        $arrsUsersUdata = json_decode($jsonUsersUdata, true);
        if(is_array($arrsUsersUdata) && $arrsUsersUdata){
            foreach($arrsUsersUdata as $arrUsersUdata){
                if($ret < $arrUsersUdata['wid']){
                    $ret = $arrUsersUdata['wid'];
                } 
            }
        }
        return $ret;
    }
    /**
     *\brief Desciption
     * This method will get maximun pageNumber from jsonUsersPdata
     */
    protected function getMaxPageNumberByJsonUsersPdata($jsonUsersPdata){
        $ret = null;
        $arrsUsersPdata = json_decode($jsonUsersPdata, true);
        if(is_array($arrsUsersPdata) && $arrsUsersPdata){
            foreach($arrsUsersPdata as $arrUsersPdata){
                if($ret < $arrUsersPdata['pageNumber']){
                    $ret = $arrUsersPdata['pageNumber'];
                } 
            }
        }
        return $ret;
    }
    /**
     *\brief Desciption
     * This method is insert 2 record to Table CONTAINER on Migration WF20 to WF30
     */
    protected function insertDefaultRecordToTableContainer(){
        global $wfconf_default_user_cdata;
        $arrsDefaultUserCdata = json_decode($wfconf_default_user_cdata, true);
        if(is_array($arrsDefaultUserCdata) && $arrsDefaultUserCdata){
            foreach($arrsDefaultUserCdata as $arrDefaultUserCdata){
                $assocData = array();
                $assocData['container_id'] = $arrDefaultUserCdata['container_id'];
                $assocData['container_name'] = $arrDefaultUserCdata['container_name'];
                $assocData['container_param'] = json_encode($arrDefaultUserCdata['container_param']);
                $assocData['container_type_name'] = isset($arrDefaultUserCdata['container_type_name']) ? $arrDefaultUserCdata['container_type_name'] : '';
                
                $objParameter = $this->objFrameworkTo->getParameter();
                $objParameter->appendFrom('CONTAINER');
                $objParameter->setAssocData($assocData);
                $this->objFrameworkTo->getDataObject()->queryInsertByParameter($objParameter);
            }
        }
    }
    protected function initColDataMigrate(){
        // new WFDataMigrate($strTableName, $strColNamePK, $strFuncNameConvertArrDataFrom)
        $objDataMigrate = new WFDataMigrate('CONTAINER', 'container_id');
        $objDataMigrate->setIsDataMigrate(false); //do not do the data migrate
        $objDataMigrate->setFuncNameCallBack('insertDefaultRecordToTableContainer');
        $this->colDataMigrate->set('CONTAINER',$objDataMigrate);
        
        // The order should migrate userdata first, then users, otherwise will cause migrate error
        $this->colDataMigrate->set('userdata',new WFDataMigrate('userdata', 'id'));
        $this->colDataMigrate->set('users',new WFDataMigrate('users', 'id', 'getArrDataUsers20to30ByArrDataFrom'));
        
        $objDataMigrate = new WFDataMigrate('serverlist', 'id');
        $objDataMigrate->setAssocSkipRecordColNameWithColValue('name', 'localhost');
        $this->colDataMigrate->set('serverlist',$objDataMigrate);
        
        //$objDataMigrate = new WFDataMigrate('serverlisttype', 'id');
        //$objDataMigrate->setAssocSkipRecordColNameWithColValue('type', 'modSimple');
        //$this->colDataMigrate->set('serverlisttype',$objDataMigrate);
        
        $this->colDataMigrate->set('proxylist',new WFDataMigrate('proxylist', 'id'));
        /*$this->colDataMigrate->set('WIDGET',new WFDataMigrate('WIDGET', 'widget_id'));
        $this->colDataMigrate->set('WIDGET_CATEGORY',new WFDataMigrate('WIDGET_CATEGORY', 'category_id'));
        $this->colDataMigrate->set('WIDGET_CATEGORY_MAPPING',new WFDataMigrate('WIDGET_CATEGORY_MAPPING', 'mapping_id'));
        $this->colDataMigrate->set('WIDGET_COMPONENT',new WFDataMigrate('WIDGET_COMPONENT', 'widget_id'));
        $this->colDataMigrate->set('WIDGET_COMPONENT_CATEGORY',new WFDataMigrate('WIDGET_COMPONENT_CATEGORY', 'category_id'));
        $this->colDataMigrate->set('WIDGET_COMPONENT_CATEGORY_MAPPING',new WFDataMigrate('WIDGET_COMPONENT_CATEGORY_MAPPING', 'mapping_id'));*/
        $objDataMigrate = new WFDataMigrate('WP_UPDATE_LOG', 'log_id');
        $objDataMigrate->setAssocSkipRecordColNameWithColValue('log_id', '1');
        $this->colDataMigrate->set('WP_UPDATE_LOG',$objDataMigrate);
        $this->colDataMigrate->set('WP_UPDATE_FILTER',new WFDataMigrate('WP_UPDATE_FILTER', 'filter_id'));
    }
}